﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Core;

namespace test.XcoAppSpaces.Core.WithSyncContext
{
    public partial class Form1 : Form
    {
        private Port<int> myport = new Port<int>();

        public Form1(XcoAppSpace space)
        {
            InitializeComponent();

            Console.WriteLine("win thread: {0}", Thread.CurrentThread.GetHashCode());
            space.ReceiveSequentially(myport, HandlePortElement,SyncContext.UseCurrentSyncContext);
        }

        void HandlePortElement(int i)
        {
            Console.WriteLine("handler thread: {0}, n causalities: {1}", Thread.CurrentThread.GetHashCode(), Dispatcher.ActiveCausalities.Count);
            textBox1.Text = i.ToString();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            ICausality c = new Causality("c" + DateTime.Now.ToString());
            Dispatcher.AddCausality(c);
            myport.Post(DateTime.Now.Second);
            //Dispatcher.RemoveCausality(c);
        }
    }
}
